package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.book.mapper.BookMapper;
import com.itany.book.mapper.ExamineMapper;
import com.itany.book.pojo.Book;
import com.itany.book.pojo.Examine;
import com.itany.book.pojo.ManagerUser;
import com.itany.book.service.ExamineService;
import com.itany.book.utils.SFTPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/25 9:52
 * Describe:
 * version:1.0
 */

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ExamineServiceImpl implements ExamineService {

    @Autowired
    ExamineMapper examineMapper;

    @Autowired
    BookMapper bookMapper;

    @Override
    public PageInfo<Examine> findByPage(Integer id, Integer page, Integer rows, String bookname,
                                        String isbn) {
        PageHelper.startPage(page,rows);
        List<Examine> users = examineMapper.selectBySupplierid(id,bookname,isbn);
        PageInfo<Examine> pageInfo = new PageInfo<>(users);
        return pageInfo;

    }

    @Override
    public void addExamine(Book book, Examine examine, MultipartFile file) {
        //1.根据isbn传书籍
        // 1.1没查到
        //  插入书籍
        //  插入examine
        //1.2 查到了，取到id
        // 插入examine

        //判断file的isempty
        // 如果是空：
            //根据book里面的url上传
        //不空。。。正常走

        Book b = bookMapper.selectByISBN(book.getIsbn());
        System.out.println("book:"+ b );
        if (b == null){
            // 上传文件到服务器
            String fileName = file.getOriginalFilename();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String  uploadDir ="yzy/" +dateFormat.format(new Date())+"/";
            InputStream is = null;
            //文件不为空
            if (!file.isEmpty()){
                fileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
                try {
                    is = file.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //如果上传的文件为空，则使用第三方的照片
            }else {
                // 获取图片的url路径
                String url = book.getImgurl();
                //设置图片名字
                fileName = System.currentTimeMillis()+".jpg";
                try {
                    URL urlAdd = new URL(url);
                    is = new DataInputStream(urlAdd.openStream());
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("第三方也没有图片");
                    uploadDir ="";
                    fileName="";
                }
            }
            //上传sftp服务器。
            try {
                SFTPUtils.sftpPut("192.168.4.8","tyh","123",22,uploadDir,is,fileName);
            } catch (Exception e) {
                // e.printStackTrace();
                System.out.println(e.getMessage());
                System.out.println("访问远程文件服务器出错！");
            }

            // 2.获取上传路径
            String imgurl = uploadDir + fileName;

            // 那这个返回的bookid是什么
            book.setImgurl(imgurl);
            // 之前没有记录，第一次添加，还未审核的时候，数量一定为0
            book.setNumber(0);
            bookMapper.insertBook(book);
            examine.setBookid(book.getId());
            examineMapper.insert(examine);
            return;
        }

        //查到
        examine.setBookid(b.getId());
        examineMapper.insert(examine);
        return;
    }

    @Override
    public void modifyExamine(Examine examine) {
        // 1.修改t_examine状态，
        // 1.1如果通过:
        // 修改书籍t_book数量(原来的+新的)和状态，1
        // 1.1.1如果查到t_book_supplier：
        // 修改t_book_supplier数量，
        // 1.1.23没查到
        //         插入t_book_supplier数量
        // 1.2 如果没有通过：(不写也可以)
        //         修改书籍t_book状态 -1

        //1.
        examineMapper.updateExamineFlagAndInfo(examine);

        if (examine.getFlag()!=1){
            return;
        }
        // 书籍数量为累加，
        bookMapper.updateBookStatusAndNumber(examine.getBookid(),examine.getNumber(),examine.getFlag());

        //
        Examine e = examineMapper.selectBySupplierIdAndBookId(examine);
        if (e!=null){
            examineMapper.updateBookSupplier(examine.getNumber(),e.getId());
        }else {
            examineMapper.insertBookSupplier(examine);
        }


    }
}
